var canvas_cnt = 0;

//swfobject.embedSWF("static/DegrafaPieMenu.swf", "myFlashContent", "960", "240", "9.0.28", "static/expressInstall.swf");
//google.load('visualization', '1', {'packages':['table', 'linechart']});
var map=null;

function handleQueryResponse(response) {
  //alert(response);
  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }
  var data = response.getDataTable();
  var chart = new google.visualization.Table(document.getElementById('chart_div'));
  chart.draw(data, {width: 480, height: 250, showRowNumber: true});
  //var visualization = new google.visualization.LineChart(document.getElementById('chart_div'));
  //visualization.draw(data, {width: 400, height: 240, legend: 'bottom'});
}

function drawChart() {
  var query = new google.visualization.Query('http://samhooxee.appspot.com/movie');
  //var query = new google.visualization.Query(
  //      'http://spreadsheets.google.com/tq?key=pCQbetd-CptGXxxQIG7VFIQ&pub=1');
  //query.setQuery('SELECT A,D WHERE D > 100 ORDER BY D');
  query.send(handleQueryResponse);
}

function on_next_click(event){
  $.post(this.href, "", function(data){
      $("#middle_1122A").html(data);
      $(".movielink").click(on_next_click);
  });
  return false;
}

function on_tianqi_click(event){
  return do_tianqi_click();
}

function on_game_click(event){
  $.get("/game", " ", function(data){
      $("#maingame_container").html(data);
      $("#gameslider1").slider({
			value:1,
			min: 0,
			max: 5,
			step: 1
      });
      $("#game_button0").button().click(function() {
	$.get("/static/maincanvas4_plus.js", function(data){
	  $("#game0").fadeOut("normal",function(){
	    $(this).remove();
	    $("#gamebox").append('<canvas id="game0"></canvas>');
	    Processing($("#game0")[0],data);
	    $("#game0").fadeIn("slow");
	  });
	});
	return false;
      });
      $("#game_button1").button().click(function() {
	$.get("/static/maincanvas4_plus.js", function(data){
	  $("#game1").fadeOut("normal",function(){
	    $(this).remove();
	    $("#gamebox").append('<canvas id="game1"></canvas>');
	    Processing($("#game1")[0],data);
	    $("#game1").fadeIn("slow");
	  });
	});
	return false;
      });
      $("#game_button2").button().click(function() {
	$.get("/static/maincanvas4_plus.js", function(data){
	  $("#game2").fadeOut("normal",function(){
	    $(this).remove();
	    $("#gamebox").append('<canvas id="game2"></canvas>');
	    Processing($("#game2")[0],data);
	    $("#game2").fadeIn("slow");
	  });
	});
	return false;
      });
      $("#game_button3").button().click(function() {
	$.get("/static/maincanvas4_plus.js", function(data){
	  $("#game3").fadeOut("normal",function(){
	    $(this).remove();
	    $("#gamebox").append('<canvas id="game3"></canvas>');
	    Processing($("#game3")[0],data);
	    $("#game3").fadeIn("slow");
	  });
	});
	return false;
      });
      $("#game_button4").button().click(function() {
	$.get("/static/maincanvas4_plus.js", function(data){
	  $("#game4").fadeOut("normal",function(){
	    $(this).remove();
	    $("#gamebox").append('<canvas id="game4"></canvas>');
	    Processing($("#game4")[0],data);
	    $("#game4").fadeIn("slow");
	  });
	});
	return false;
      });
      $.get("/static/maincanvas4.js", function(data){
	Processing($("#game0")[0], data);
	Processing($("#game1")[0], data);
	Processing($("#game2")[0], data);
	Processing($("#game3")[0], data);
	Processing($("#game4")[0], data);
      });
  });
  return false;
}

function do_tianqi_click(){
  $.get("/tianqi", " ", function(data){
      $("#middle_1122A").html(data).accordion({
	      autoHeight: false,
	      navigation: true
	    });
  });
  //should not loadmap multiple times
  $("#map_container").fadeIn();
  $("#map_google").addClass("map-box");
  ///*
  if (map==null){
    map = loadmap();
    $.get("/controlCityGet", "", function(data){
      $("#city").append("<p>start</p>");
      var batch = [];
      $(data).find("city").each(function(){
	var geo = $(this).find("geo").text().split(",");
	var name = $(this).find("name").text();
	var woeid = $(this).find("woeid").text();
	var point = new GLatLng(parseFloat(geo[0]), parseFloat(geo[1]));
	var marker = new GMarker(point);
	GEvent.addListener(marker, "click", function() {
	  marker.openInfoWindowHtml("<b>" + name + "</b>");
	  $.get("/tianqiYahooCity", {"woeid": woeid, "name": name}, function(data){
	    $("#middle_1122A").prepend(data);
	    $("#middle_1122A").accordion("destroy").accordion({
	      autoHeight: false,
	      navigation: true
	    });
	  });
	});
	batch.push(marker);
      });
      mgr = new MarkerManager(map);
      mgr.addMarkers(batch, 6);
      mgr.refresh();
      //var point = new GLatLng(30.498211,117.04702);
      //map.addOverlay(new GMarker(point));
      $("#city").append("<p>done</p>");
    });
  }
  //*/
  return false;
}

function loadmap() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_google"));
    map.setCenter(new GLatLng(31.86141,117.27562), 6);
    map.addControl(new GSmallMapControl());
    return map;
  }
}

$(function(){
  $(".movielink").click(on_next_click);
  $(".tianqi").click(on_tianqi_click);
  $("#maingame").click(on_game_click);
  $("#map_container").hide();    
  //google.setOnLoadCallback(drawChart);
  do_tianqi_click();
  //Processing($("#maincanvas1"), $("#maincanvas1_script").text);
  //Processing($("#maincanvas2"), $("#maincanvas2_script").text);
  $("#maincanvas1").draggable();
  $("#maincanvas4").draggable({ grid: [20,20] });
  $("#maincanvas5").draggable({ grid: [20,20] });
  //_admob.fetchAd($("#admob_ad"));
  _admob.fetchAd(document.getElementById('admob_ad'));
});